package de.koller.million.grid.frame.ply;

import de.koller.million.grid.engine.player.Player;
import de.koller.million.grid.frame.MainFrame;
import de.koller.aframe.util.thread.ICommand;
import java.util.ArrayList;
import java.util.List;

public class PlayerFrameOpener implements ICommand {
	
	private final MainFrame frame;
	private final String name;

	public PlayerFrameOpener( MainFrame frame, String name ) {
		this.frame = frame;
		this.name = name;
	}

	@Override
	public void handle( Object... params ) {
			
		Player s = null;
		for( Player p : frame.groot.engine.playerContainer.getPlayers() )
			if( p.name.equals( name ) ) {
				s = p;
				break;
			}
		
		if( s == null ) {
			System.out.println( "Spieler mit Name '" + name + "' nicht gefunden." );
			return;
		}
		
		frame.addChild( new PlayerFrame( s ) );
	}
	
	private void openRandomPlayer() {
		List<Player> l = new ArrayList<>();
		l.addAll( frame.groot.engine.playerContainer.getPlayers() );
		
		Player s = l.get( frame.groot.rnd.nextInt( l.size() ) );
		frame.addChild( new PlayerFrame( s ) );
	}
	
}
